package com.example.ssm.service.impl;

import com.example.ssm.vo.QueryVo;
import com.example.ssm.mapper.CustomerMapper;
import com.example.ssm.pojo.Customer;
import com.example.ssm.pojo.CustomerExample;
import com.example.ssm.service.ICustomerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("customerService")
public class CustomerServiceIImpl implements ICustomerService {
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public List<Customer> list()
    {
        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();
        return customerMapper.selectByExample(null);
    }

    @Override
    public PageInfo<Customer> list(QueryVo queryVo) {


        PageHelper.startPage(queryVo.getPage(),queryVo.getRows());
        CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria criteria = customerExample.createCriteria();


        if(queryVo.getCustName()!=null&&queryVo.getCustName().length()>0)
            criteria.andCustNameLike("%"+queryVo.getCustName()+"%");
        if(queryVo.getCustSource()!=null&&queryVo.getCustSource().length()>0)
            criteria.andCustSourceEqualTo(queryVo.getCustSource());
        if(queryVo.getCustIndustry()!=null&&queryVo.getCustIndustry().length()>0)
            criteria.andCustIndustryEqualTo(queryVo.getCustIndustry());
        if(queryVo.getCustLevel()!=null&&queryVo.getCustLevel().length()>0)
            criteria.andCustLevelEqualTo(queryVo.getCustLevel());


        List<Customer> list = customerMapper.selectByExample(customerExample);

        PageInfo<Customer> pageInfo = new PageInfo<>(list);

        return pageInfo;
    }

    @Override
    public Customer queryCustomerById(Long id) {
        return customerMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateCustomerById(Customer customer) {
        customerMapper.updateByPrimaryKeySelective(customer);
    }

    @Override
    public void deleteCustomerById(Long id) {
        customerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void add(Customer customer) {
        customerMapper.insert(customer);
    }

}
